package com.wc.算法基础课.C第三讲搜索与图论.spfa.spfa判断负环;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/14 14:34
 * @description https://www.acwing.com/problem/content/854/
 */
public class Main {
    /**
     * 思路：spfa是可以判断有负权边的最短路
     * 负环怎么判断呢, 那就想到多存一个到达每一个点的最短路的边数, 是不是如果超过n - 1了, 那就说明有负环了,
     * 说明负环是可以无限的延长最短路的边数, 导致到达的最短路
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 2010, M = 10010;
    static int[] h = new int[N];
    static int[] w = new int[M];
    static int[] cnt = new int[N];
    static int[] dist = new int[N];
    static int INF = 0x3f3f3f3f;
    static int[] ne = new int[M];
    static int[] e = new int[M];
    static int idx = 1;
    static int n, m;
    // 判断这个点是否在队列当中
    static boolean[] st = new boolean[N];
    static Queue<Integer> q = new LinkedList<>();

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(a, b, c);
        }
        if (spfa()) out.println("Yes");
        else out.println("No");
        out.flush();
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static boolean spfa() {
        // 不用初始化, 是因为有负环, 会优先走负环那里,一直绕
        for (int i = 1; i <= n; i++) {
            q.add(i);
            st[i] = true;
        }

        while (!q.isEmpty()) {
            int t = q.poll();
            st[t] = false;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    cnt[j] = cnt[t] + 1;
                    if (cnt[j] == n) return true;
                    if (!st[j]) {
                        q.add(j);
                        st[j] = true;
                    }
                }
            }
        }
        return false;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}